﻿using NLog;
using System;
using System.ServiceModel;
using System.ServiceProcess;

namespace Ayatta.Service.Product.Index
{
    public class WindowsService : ServiceBase
    {
        private ServiceHost serviceHost;
        private readonly Logger logger = LogManager.GetLogger("log");
        public WindowsService()
        {
            AutoLog = false;
            CanPauseAndContinue = false;
            ServiceName = Program.ServiceName;
        }

        protected override void OnStart(string[] args)
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
            }
            try
            {
                serviceHost = new ServiceHost(typeof(ProductIndexService));
                serviceHost.Opened += ServiceOpended;
                serviceHost.Closed += ServiceCloesed;
                serviceHost.Open();
            }
            catch (Exception e)
            {
                logger.Warn(e.Message);
            }

        }

        protected override void OnStop()
        {
            if (serviceHost == null) return;
            serviceHost.Close();
            serviceHost = null;
        }

        protected void ServiceOpended(object sender, EventArgs e)
        {
            logger.Trace("ProductIndexService Opended");
        }

        protected void ServiceCloesed(object sender, EventArgs e)
        {
            logger.Trace("ProductIndexService Cloesed");
        }
    }
}